package vn.vfriends.vbox.bean;

import io.buji.pac4j.ShiroWebContext;
import java.io.IOException;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.env.EnvironmentLoader;
import org.apache.shiro.web.env.IniWebEnvironment;
import org.pac4j.oauth.client.Google2Client;
import org.pac4j.oauth.client.WindowsLiveClient;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vbox.model.User;

/**
 *
 * @author tuancn
 */
@Named
@SessionScoped
public class SecurityBean implements Serializable {
    
    private static final Logger logger = LoggerFactory.getLogger(SecurityBean.class);
    
    public String getName() {
        User loggedUser = (User)SecurityUtils.getSubject().getSession().getAttribute("LOGGED_USER");
        if(loggedUser != null) {
            return loggedUser.getName();
        } else {
            return "";
        }
    }
    
    public String getAuthenticationUrl(String provider) {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest hReq = (HttpServletRequest)context.getExternalContext().getRequest();
        HttpServletResponse hRes = (HttpServletResponse)context.getExternalContext().getResponse();
        ShiroWebContext shiroContext = new ShiroWebContext(hReq, hRes);
        String url;
        if(provider.equals("GOOGLE")) {
            Google2Client google2Client = this.getClient(Google2Client.class, "googleClient");
            url = google2Client.getRedirectionUrl(shiroContext);
        } else {
            WindowsLiveClient windowsLiveClient = this.getClient(WindowsLiveClient.class, "windowsLiveClient");
            url = windowsLiveClient.getRedirectionUrl(shiroContext);
        }
        return url;
    }
    
    private <T extends Object> T getClient(Class<T> clazz, String name) {
        FacesContext context = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
        IniWebEnvironment env = (IniWebEnvironment) servletContext.getAttribute(EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY);
        if (env != null) {
            return env.getObject(name, clazz);
        }
        return null;
    }
    
    public void preRenderLoginView() {
        if(SecurityUtils.getSubject().isAuthenticated()) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("home.jsf");
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            }
        }
    }
}
